package com.zhong.scanerdemo;

import java.util.Calendar;

import com.zhong.scanerdemo.model.UploadMsg;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

public class MainActivity extends Activity implements View.OnTouchListener {
	private final String TAG = "MainActivity";
	private SharedPreferences sharedPreferences;
	private SharedPreferences.Editor editor;
	private Button bt_confirm;
	// private Button bt_testRequest;
	private EditText et_dutyOfficer, et_driver, et_carNum, et_startingPoint,
			et_destination, et_uid, et_pwd, et_source, et_startDate, et_method,
			et_descr;

	private CheckBox cb_isUpload;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		sharedPreferences = this.getSharedPreferences("scaner",
				MODE_WORLD_READABLE);

		editor = sharedPreferences.edit();

		// 新增加要填写的信息
		et_uid = (EditText) findViewById(R.id.et_uid);
		et_pwd = (EditText) findViewById(R.id.et_pwd);
		et_source = (EditText) findViewById(R.id.et_source);
		et_startDate = (EditText) findViewById(R.id.et_startdate);
		et_method = (EditText) findViewById(R.id.et_method);
		et_descr = (EditText) findViewById(R.id.et_descr);
		et_startDate.setOnTouchListener(this);

		et_dutyOfficer = (EditText) findViewById(+R.id.et_dutyofficer);
		et_driver = (EditText) findViewById(R.id.et_driver);
		et_carNum = (EditText) findViewById(R.id.et_carnum);
		et_startingPoint = (EditText) findViewById(R.id.et_startingpoint);
		et_destination = (EditText) findViewById(R.id.et_destination);

		// 获取其他控件
		bt_confirm = (Button) findViewById(R.id.btn_test);
		cb_isUpload = (CheckBox) findViewById(R.id.cb_uploadimmediately);
		initViews();

		/*
		 * 测试方法
		 * 
		 * 
		 * bt_testRequest=(Button) findViewById(R.id.btn_testrequest);
		 * bt_testRequest.setOnClickListener(new OnClickListener() {
		 * 
		 * @Override public void onClick(View arg0) { // TODO Auto-generated
		 * method stub Thread thread = new Thread(){ public void run(){
		 * RequestTest requestTest=new RequestTest(); String
		 * result=requestTest.executeHttpGet(); ClientLogUtil.i(TAG,
		 * "result------->"+result); } }; thread.start();
		 * 
		 * } });
		 */
	}

	private void initViews() {
//		String dutyOfficer = sharedPreferences.getString("dutyOfficer", "");
//		String driver = sharedPreferences.getString("driver", "");
//		String carNum = sharedPreferences.getString("carNum", "");
//		String startingPoint = sharedPreferences.getString("startingPoint", "");
//		String destination = sharedPreferences.getString("destination", "");
//		boolean isUpload = sharedPreferences.getBoolean("isUploadIm", false);
//		// 新增
//		String uid = sharedPreferences.getString("uid", "");
//		String pwd = sharedPreferences.getString("pwd", "");
//		String source = sharedPreferences.getString("source", "");
//		String startDate = sharedPreferences.getString("startDate", "");
//		String method = sharedPreferences.getString("method", "");
//		String descr = sharedPreferences.getString("descr", "");
		UploadMsg uploadMsg=new UploadMsg();

		// 新增
		et_uid.setText(uploadMsg.getUid());
		et_pwd.setText(uploadMsg.getPwd());
		et_source.setText(uploadMsg.getSource());
		et_startDate.setText(uploadMsg.getStartDate());
		et_method.setText(uploadMsg.getMethod());
		et_descr.setText(uploadMsg.getDescr());

		et_dutyOfficer.setText(uploadMsg.getDutyOfficer());
		et_driver.setText(uploadMsg.getDriver());
		et_carNum.setText(uploadMsg.getCarNum());
		et_startingPoint.setText(uploadMsg.getStartingPoint());
		et_destination.setText(uploadMsg.getDestination());
		cb_isUpload.setChecked(uploadMsg.isUploadDirect());

		cb_isUpload.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				editor.putBoolean("isUploadIm", isChecked);
				editor.commit();
			}
		});

		bt_confirm.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// startScaning();
				MainActivity.this.finish();
			}
		});

	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		MainActivity.this.finish();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();

		editor.putString("dutyOfficer", et_dutyOfficer.getText().toString());
		editor.putString("driver", et_driver.getText().toString());
		editor.putString("carNum", et_carNum.getText().toString());
		editor.putString("startingPoint", et_startingPoint.getText().toString());
		editor.putString("destination", et_destination.getText().toString());

		// new
		editor.putString("uid", et_uid.getText().toString());
		editor.putString("pwd", et_pwd.getText().toString());
		editor.putString("source", et_source.getText().toString());
		editor.putString("startDate", et_startDate.getText().toString());
		editor.putString("method", et_method.getText().toString());
		editor.putString("descr", et_descr.getText().toString());

		editor.commit();

	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			View view = View.inflate(this, R.layout.dialog_datepicker, null);
			final DatePicker datePicker = (DatePicker) view
					.findViewById(R.id.date_picker);
			builder.setView(view);

			Calendar cal = Calendar.getInstance();
			cal.setTimeInMillis(System.currentTimeMillis());
			datePicker.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
					cal.get(Calendar.DAY_OF_MONTH), null);

			if (v.getId() == R.id.et_startdate) {
				final int inType = et_startDate.getInputType();
				et_startDate.setInputType(InputType.TYPE_NULL);
				et_startDate.onTouchEvent(event);
				et_startDate.setInputType(inType);
				et_startDate.setSelection(et_startDate.getText().length());

				builder.setTitle("选取起始日期");
				builder.setPositiveButton("确  定",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {

								StringBuffer sb = new StringBuffer();
								sb.append(String.format("%d-%02d-%02d",
										datePicker.getYear(),
										datePicker.getMonth() + 1,
										datePicker.getDayOfMonth()));
								et_startDate.setText(sb);
								dialog.cancel();
							}
						});

			}

			Dialog dialog = builder.create();
			dialog.show();
		}

		return true;
	}
}
